package com.otitan.la.forest.system.util;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * @Description: 权限工具类
 * @Author: jiachangsen
 * @CreateDate: 2020/06/28 17:30
 * @UpdateUser: jiachangsen
 * @UpdateDate: 2020/06/28 17:30
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Component
public class AuthHandler {

    @Autowired
    private RedisTemplate redisTemplate;
    @Value("${redis.key-prefix}")
    private String redisKeyPrefix;

    /**
     * 获取当前登录用户的用户名
     *
     * @param []
     * @return java.lang.String
     * @throws
     * @author jiachangsen
     * @date 2020/07/13 17:21
     */
    public String getLoginUserName() {
        String data = getLoginUser();
        JSONObject object = JSONObject.parseObject(data);
        Object name = object.get("userName");
        return Optional.of(name).orElse("sys_main").toString();
    }

    /**
     * 获取当前登录用户信息
     *
     * @param []
     * @return java.lang.String
     * @throws
     * @author jiachangsen
     * @date 2020/07/13 17:21
     */
    public String getLoginUser() {
        // 获取用户信息
        HttpServletRequest request = AutoCommonUtil.getHttpServletRequest();
        String tokenId = request.getParameter("token");
        Object data = redisTemplate.opsForValue().get(redisKeyPrefix + tokenId);
        if (data != null) {
            return data.toString();
        }
        return "";
    }


}
